package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.OrganizationDO;
import com.yuantu.judiciary.model.enums.AuthState;
import com.yuantu.judiciary.model.enums.OrganizationType;
import com.yuantu.judiciary.model.vo.OrgSourceOrganizationQueryVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;

/**
 * 企业信息数据库操作
 *
 * @author syw
 * @since 2023-08-03
 **/
public interface IOrganizationDao extends JpaRepository<OrganizationDO, Long> {

    OrganizationDO findByIdAndDeletedFalse(Long id);

    List<OrganizationDO> findByIdInAndDeletedFalse(List<Long> id);

    @Query("select o from OrganizationDO o " +
            "where (:name is null or :name ='' or o.enterName like concat('%', :name, '%')) " +
            "and (:state is null or o.authState = :state) " +
            "and (:startTime is null or o.createTime >= :startTime) " +
            "and (:endTime is null or o.createTime <= :endTime)" +
            "and o.type = :type " +
            "and (:isSource is null or (:isSource = true and o.organizationSourceId != 1) or (:isSource = false and o.organizationSourceId = 1)) " +
            "and o.deleted = false ")
    Page<OrganizationDO> findOrgByEnterNameLikeAndAuthStateAndCreateTime(
            String name, AuthState state, Timestamp startTime, Timestamp endTime,
             OrganizationType type, Boolean isSource, Pageable pageable
    );

    @Query("select o from OrganizationDO o " +
            "where (:name is null or :name ='' or o.username like concat('%', :name, '%')) " +
            "and (:state is null or o.authState = :state) " +
            "and (:startTime is null or o.createTime >= :startTime) " +
            "and (:endTime is null or o.createTime <= :endTime) " +
            "and o.type = :type and o.deleted = false ")
    Page<OrganizationDO> findOrgByUsernameLikeAndAuthStateAndCreateTime(
            String name, AuthState state, Timestamp startTime, Timestamp endTime, OrganizationType type, Pageable pageable
    );

    Integer countDistinctByTypeAndEnterNameAndDeletedFalse(OrganizationType type, String enterName);

    Integer countDistinctByUsernameAndIdNumberAndAuthStateAndDeletedFalse(String username, String idNumber, AuthState authState);

    Integer countDistinctByTypeAndUsernameAndDeletedFalse(OrganizationType type, String username);

    Integer countDistinctByTypeAndIdNumberAndDeletedFalse(OrganizationType type, String idNumber);

    OrganizationDO findByTypeAndIdNumberAndDeletedFalse(OrganizationType type, String idNumber);

    List<OrganizationDO> findOrgByAuthStateAndDeletedFalse(AuthState authState);

    OrganizationDO findByTypeAndPhoneAndDeletedFalse(OrganizationType organizationType, String phone);

    OrganizationDO findInfoByCreditCodeAndDeletedFalse(String creditCode);

    List<OrganizationDO> findInfosByOrganizationSourceIdAndDeletedFalse(Long organizationSourceId);

    @Query("select o from OrganizationDO o " +
            "where o.deleted = false " +
            "and (:#{#query.enterName} is null or :#{#query.enterName} = '' or o.enterName like concat('%', :#{#query.enterName}, '%')) " +
            "and (:#{#query.authState} is null or :#{#query.authState} = '' or o.authState = :#{#query.authState}) " +
            "and (:#{#query.registerStartTime} is null or o.createTime >= :#{#query.registerStartTime}) " +
            "and (:#{#query.registerEndTime} is null or o.createTime <= :#{#query.registerEndTime}) " +
            "and (:#{#query.orgSourceId} is null or o.organizationSourceId = :#{#query.orgSourceId}) " +
            "and o.organizationSourceId != 1 ")
    Page<OrganizationDO> findOrgSourceInfos(OrgSourceOrganizationQueryVO query, Pageable pageable);

    OrganizationDO findInfoByTypeAndEnterNameAndOrganizationSourceIdAndDeletedFalse(OrganizationType type, String enterName, Long organizationSourceId);

    List<OrganizationDO> findOrgByEnterNameAndDeletedFalse(String enterName);
}
